package com.ymatou.iapi.godmch.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.json.JSONException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.springframework.context.annotation.Bean;

import com.ymatou.iapi.godmch.parameter.AgentTypeVo;
import com.ymatou.iapi.godmch.parameter.CreateCodeBean;
import com.ymatou.iapi.godmch.parameter.createagentBean;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.parameter.getAgentTypeByModeBean;
import com.ymatou.iapi.godmch.service.getAgentTypeByModeCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.afterserviceinfo;
import com.ymttest.database.model.agent_type;
import com.ymttest.database.sqlwapper.godmch.agent_typeWapper;
import com.ymttest.database.sqlwapper.godmch.invite_codeWapper;
/**
*获取代理类型列表(代理模式查询)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getAgentTypeByMode{
	private static getAgentTypeByModeBean bean;
	private static getAgentTypeByModeCall call;
	private static CreateCodeBean ccbean = new CreateCodeBean();
	public agent_typeWapper agentTypeWapper = new agent_typeWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取代理类型列表(代理模式查询)");
	}

	@Before
	public void caseUp() {
		bean=new getAgentTypeByModeBean();
		call=new getAgentTypeByModeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	private void check_verify(Integer count) throws InterruptedException, JSONException
	{
		Map<String, Object> map = new HashMap();
		map.put("mchId", bean.getMchId());
		if(bean.getAgentMode()!=null)
		map.put("agentMode", bean.getAgentMode());
		List<agent_type> list = agentTypeWapper.selectby(map);
		
		List<AgentTypeVo> return_infos=call.getAgentTypes();
		Logger.verifyEquals(list.size(), return_infos.size(), "验证返回条数与数据库中查询一致");
		Logger.verifyEquals("200", call.getStatusCode1(), "验证返回status");
		for(int i=0;i<list.size();i++)
		{
			Logger.verifyEquals(list.get(i).getAgentType(), return_infos.get(i).getAgentType(), "验证返回的第"+(i+1)+"条AgentType与数据库中的AgentType一致");
			Logger.verifyEquals(list.get(i).getDiscount().stripTrailingZeros().toPlainString(), return_infos.get(i).getDiscount().stripTrailingZeros().toPlainString(), "验证返回的第"+(i+1)+"条Discount与数据库中的Discount一致");
			Logger.verifyEquals(list.get(i).getTypeTitle(), return_infos.get(i).getTitle(), "验证返回的第"+(i+1)+"条Title与数据库中的Title一致");
			Logger.verifyEquals(list.get(i).getSeqNo(), return_infos.get(i).getSeqNo(), "验证返回的第"+(i+1)+"条SeqNo与数据库中的SeqNo一致");
			
			Logger.verifyEquals(list.get(i).getRemark(), return_infos.get(i).getRemark(), "验证返回的第"+(i+1)+"条Remark与数据库中的Remark一致");
			Logger.verifyEquals(list.get(i).getAgentMode(), return_infos.get(i).getAgentMode(), "验证返回的第"+(i+1)+"条AgentMode与数据库中的AgentMode一致");
			Logger.verifyEquals(list.get(i).getRebateRate().stripTrailingZeros().toPlainString(), return_infos.get(i).getRebateRate().stripTrailingZeros().toPlainString(), "验证返回的第"+(i+1)+"条RebateRate与数据库中的RebateRate一致");
			//usedAgent
			Optional<AgentTypeVo> UsedAgent=call.getAgentTypes().stream().filter(at->at.getAgentType().equals(ccbean.getRoleType())).findAny();
			Logger.verifyEquals(count, !UsedAgent.equals(Optional.empty())?UsedAgent.get().getUsedAgent():0, "验证已使用人数UsedAgent值");
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_001() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证代理模式包含2 - 自动继承（分销小店）,传入agentMode 1 - 自主选货（分销代理）");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=1;
			for(int i=0;i<count;i++) {
				Integer agentid=GodmchManageCallService.CreateSellerMch(code);
			}
			int mchId = ccbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(2);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			bean.setMchId(mchId);
			bean.setAgentMode(1);
			call.setData(bean);
			call.callService();			
			
			check_verify(count);
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_002() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证不代理模式包含2-自动继承（分销小店）,获取1-自主选货(分销代理)");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=1;
			for(int i=0;i<count;i++) {
				Integer agentid=GodmchManageCallService.CreateSellerMch(code);
			}
			int mchId = ccbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(2);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			bean.setMchId(mchId);
			bean.setAgentMode(1);
			call.setData(bean);
			call.callService();			
			
			check_verify(count);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_003() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证代理模式包含1自主选货（分销代理）,获取2-自动继承(分销小店)");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=0;
			int mchId = ccbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(2);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			bean.setMchId(mchId);
			bean.setAgentMode(2);
			call.setData(bean);
			call.callService();			
			
			check_verify(count);			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_004() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证商户仅有1-自主选货(分销代理),获取2-自动继承(分销小店)-返回空");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=0;
			int mchId = ccbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(1);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			bean.setMchId(mchId);
			bean.setAgentMode(2);
			call.setData(bean);
			call.callService();			
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证返回status");		
			Logger.verifyEquals("{}", call.getResult(), "验证返回Result");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_005() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证商户不存在");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=0;
			int mchId = ccbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(1);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			bean.setMchId(mchId+11);
			bean.setAgentMode(2);
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("200", call.getStatusCode1(), "验证返回status");
			Logger.verifyEquals("{}", call.getResult(), "验证返回Result");
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_006() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证活商户为空");
		try {
			//bean.setMchId(mchId+11);
			bean.setAgentMode(2);
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证返回status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_007() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证代理模式agentMode不存在");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=0;
			int mchId = ccbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(1);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			bean.setMchId(mchId);
			bean.setAgentMode(99);//不存在
			call.setData(bean);
			call.callService();				
			
			Logger.verifyEquals("4001", call.getStatusCode1(), "验证返回status");		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getAgentTypeByMode_008() {
		Logger.start(true,"获取代理类型列表(代理模式查询)-验证代理模式agentMode为空-返回全部");
		try {
			//新增渠道商
			ccbean = GodmchManageCallService.CreateAllMsgSellerMch();
			ccbean.setAgentModel(2);
			String code = GodmchManageCallService.createCodeCall(ccbean).getCode();
			int count=0;
			int mchId = ccbean.getMchId();
			
			createagentBean createagentBean=GodmchManageCallService.InitCreateAgentTypeBean(mchId);
			createagentBean.setAgentMode(1);//1 - 自主选货（分销代理） 2 - 自动继承（分销小店）
			GodmchManageCallService.CreateAgentTypeCall(createagentBean);
			
			bean.setMchId(mchId);
			//bean.setAgentMode(1);
			call.setData(bean);
			call.callService();				
			
			check_verify(count);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}